const tabs = {
    state: {
        // 标签页
        tabs: JSON.parse(sessionStorage.getItem("tabs")) || []
    },
    getters: {
        getTabs: state => state.tabs
    },
    mutations: {
        //添加一个Tab页
        createTab: (state, data) => {
            // 获取菜单(主菜单和子菜单)
            const menu = JSON.parse(sessionStorage.getItem("menus"));
            // 判断展开标签页或首页 如果是已经展开的就不添加了
            if (!menu || state.tabs.some(name => name["path"] === data) || data === "/") {
                return false;
            }
            // 主菜单路径
            const path = `/${data.split("/")[1]}`;
            // 获取子菜单
            const childMenu = menu.filter(name => name["path"] === path)[0]["children"];
            let tab;
            if (childMenu) {
                tab = childMenu.filter(name => name["path"] === data)[0]; // 当前标签页
            } else {
                tab = menu.filter(name => name["path"] === data)[0]; // 当前标签页
            }
            state.tabs.push({
                name: tab["name"],
                path: tab["path"]
            });
            // 添加当前标签页
            sessionStorage.setItem("tabs", JSON.stringify(state.tabs));
        },
        //关闭tab操作
        closeTabs: (state, data) => {
            switch (data[0]) {
                case "closeAllTabs":
                    // 关闭所有标签页
                    state.tabs = [];
                    break;
                case "closeOtherTabs":
                    // 关闭其它标签页
                    state.tabs = state.tabs.filter(name => name["path"] === data[1]);
                    break;
                default:
                    // 关闭当前标签页
                    state.tabs = state.tabs.filter(name => name["path"] !== data);
            }
            // 更新当前标签页
            sessionStorage.setItem("tabs", JSON.stringify(state.tabs));
        },
        //重置标签页
        tabsReset: state => {
            state.tabs = [];
        }
    }
};
export default tabs;
